package com.FH.organization.siperintend_item;

import com.FH.organization.department.DepartmentResource;
import com.FH.organization.people.KamMemberResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/12/13.
 */
@Stateless
public class SiperintendItemImp extends AccessDB implements SiperintendItemDAO {
    @Inject
    KamMemberResource kamMemberResource;
    @Inject
    DepartmentResource departmentResource;

    @Override
    public ResultDTO getSiperintendItemInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from siperintend_item where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("siperintend__itemid")) {
                sql_w += " AND siperintend__itemid = '" + json.getString("siperintend__itemid") + "'";
            }
            if (json.has("siperintend_id")) {
                sql_w += " AND siperintend_id ='" + json.getString("siperintend_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<SiperintendItemShowDTO> result = new ArrayList<SiperintendItemShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select siperintend_itemid,work_task,progress_situation,plan_finish_time,duty_leader,duty_organization,duty_men from siperintend_item where 1=1 " + sql_w + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    SiperintendItemShowDTO siperintendItemShowDTO = new SiperintendItemShowDTO();
                    siperintendItemShowDTO.setSiperintend_itemid(seresult.get(i)[1]);
                    siperintendItemShowDTO.setWork_task(seresult.get(i)[2]);
                    siperintendItemShowDTO.setProgress_situation(seresult.get(i)[3]);
                    siperintendItemShowDTO.setPlan_finish_time(seresult.get(i)[4]);
                    siperintendItemShowDTO.setDuty_leader(seresult.get(i)[5]);
                    siperintendItemShowDTO.setDuty_organization(seresult.get(i)[6]);
                    siperintendItemShowDTO.setDuty_men(seresult.get(i)[7]);

                    ResultDTO duty_leader = null, duty_men = null, dety_org = null;

                    duty_leader = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[5] + "}", "1", "1");
                    siperintendItemShowDTO.setDuty_leaderlist(duty_leader.getValues());

                    dety_org = departmentResource.getDepartmentInfo("{organization_id:" + seresult.get(i)[6] + "}", "1", "1");
                    siperintendItemShowDTO.setDuty_organizationlist(dety_org.getValues());

                    duty_men = kamMemberResource.getUserListInfo("{member_id:" + seresult.get(i)[7] + "}", "1", "1");
                    siperintendItemShowDTO.setDuty_menlist(duty_men.getValues());

                    result.add(siperintendItemShowDTO);

                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
                //获取上报人


            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    @Override
    public ResultDTO addSiperintendItemInfo(SiperintendItemPullDTO siperintendItemPullDTO) {
        String sql_isHas = "select count(1) from siperintend_item WHERE siperintend_itemid='" + siperintendItemPullDTO.getSiperintend_itemid() + "'";

        String existName = getReportingToOne(sql_isHas);//插入前，查询是否有重复字段

        if (!existName.equals("0")) {
            return updateSiperintendItemInfo(siperintendItemPullDTO);
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式化

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        int resultls = 0;

        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();

        if (existName.equals("0")) {

            String sql = "";

            //构建插入sql
            sql += "INSERT  INTO siperintend_item(siperintend_itemid,work_task,progress_situation,plan_finish_time,duty_leader,duty_organization,duty_men,siperintend_id,type) " +
                    "VALUES('" + uuid + "','" + siperintendItemPullDTO.getWork_task() + "','" + siperintendItemPullDTO.getProgress_situation() + "','"
                    + siperintendItemPullDTO.getPlan_finish_time() + "','" + siperintendItemPullDTO.getDuty_leader() + "','" + siperintendItemPullDTO.getDuty_organization() + "','" + siperintendItemPullDTO.getDuty_men() + "','"
                    + siperintendItemPullDTO.getSiperintend_id() + "','1')";
            //插入业务表
            resultls = addReportingTo(sql);

        }
        if (resultls != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }

        return resultDTO;
    }

    @Override
    public ResultDTO updateSiperintendItemInfo(SiperintendItemPullDTO siperintendItemPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        String siperintenditems = siperintendItemPullDTO.getSiperintenditems();
        int result=0;
        try {
            JSONArray array = new JSONArray(siperintenditems);
            if (array != null && array.length() > 0) {
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    String siperintend_itemid=jsonObject.getString("siperintend_itemid");
                    String siperintend_id=jsonObject.getString("siperintend_id");
                    String progress_situation=jsonObject.getString("progress_situation");
                    String QUERY = "";

                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String time = df.format(new Date());
                    //更新sql语句
                    QUERY += "update siperintend_item set updatetime ='" + time + "'";

                    if (null !=progress_situation&& !"".equals(progress_situation)) {
                        QUERY += ",progress_situation = '" + progress_situation + "'";
                    }
                    QUERY += " where siperintend_id = '" + siperintendItemPullDTO.getSiperintend_id() + "' and siperintend_itemid='"+siperintend_itemid+"'";
                    result = this.addReportingTo(QUERY);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }

        return resultDTO;
    }

    @Override
    public ResultDTO deleteSiperintendItemInfo(SiperintendItemPullDTO siperintendItemPullDTO) {
        //删除之前查询一下该表是否有记录
        String sql = "select count(1) from siperintend_item  where siperintend_itemid='" + siperintendItemPullDTO.getSiperintend_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        if (!existName.equals(0)) {
            String QUERY = "";
            QUERY += "delete from siperintend_item where siperintend_itemid='" + siperintendItemPullDTO.getSiperintend_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        ResultDTO resultDTO = new ResultDTO();
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
